package controller

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"pharymacy-management-system/dao/mysql"
	"pharymacy-management-system/logic"
	"pharymacy-management-system/models"
	"pharymacy-management-system/network"
)

func SignUpHandler(c *gin.Context) {
	// 1.参数校验
	var p models.ParamSignUp
	if err := c.ShouldBindJSON(&p); err != nil {
		zap.L().Error("SignUp with invalid param", zap.Error(err))
		// 判断err是不是validator.ValidationErrors类型
		var errs validator.ValidationErrors
		ok := errors.As(err, &errs)
		if !ok {
			network.ResponseError(c, network.CodeInvalidParam)
			return
		}
		network.ResponseErrorWithMsg(c, network.CodeInvalidPassword, removeTopStruct(errs.Translate(trans)))
		return
	}
	// 2.业务处理
	if err := logic.SignUp(&p); err != nil {
		if err.Error() == mysql.ErrorUserExist {
			network.ResponseError(c, network.CodeUserExist)
			return
		}
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	// 3.返回结果
	network.ResponseSuccess(c, nil)
}

func LoginHandler(c *gin.Context) {
	// 1.参数校验
	var p models.ParamLogin
	if err := c.ShouldBindJSON(&p); err != nil {
		zap.L().Error("Login with invalid param", zap.Error(err))
		// 判断err是不是validator.ValidationErrors类型
		var errs validator.ValidationErrors
		ok := errors.As(err, &errs)
		if !ok {
			network.ResponseError(c, network.CodeInvalidParam)
			return
		}
		network.ResponseErrorWithMsg(c, network.CodeInvalidParam, removeTopStruct(errs.Translate(trans)))
		return
	}

	// 2.业务逻辑处理
	token, err := logic.Login(&p)
	if err != nil {
		zap.L().Error("logic.Login failed", zap.Error(err))
		if err.Error() == mysql.ErrorUserNotExist {
			network.ResponseError(c, network.CodeUserNotExist)
			return
		}
		network.ResponseError(c, network.CodeInvalidPassword)
		return
	}
	// 3.返回响应
	m := map[string]interface{}{}
	m["token"] = token
	network.ResponseSuccess(c, m)
}

func InfoHandler(c *gin.Context) {
	userID, err := network.GetCurrentUser(c)
	if err != nil {
		network.ResponseError(c, network.CodeNeedLogin)
		return
	}
	user, err := logic.GetUserByUserID(userID)
	if err != nil {
		zap.L().Error("logic.GetUserInfo failed", zap.Error(err))
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	m := map[string]interface{}{}
	m["user"] = user
	network.ResponseSuccess(c, m)
}

func UpdateNickNameHandler(c *gin.Context) {
	userNickName := c.Param("nickname")
	if userNickName == "" || len(userNickName) == 0 {
		network.ResponseErrorWithMsg(c, network.CodeInvalidParam, "用户昵称不能为空")
		return
	}
	userID, err := network.GetCurrentUser(c)
	if err != nil {
		network.ResponseError(c, network.CodeNeedLogin)
		return
	}
	user := models.User{
		UserID:       userID,
		UserNickName: userNickName,
	}
	err = logic.UpdateUserNickName(&user)
	if err != nil {
		network.ResponseError(c, network.CodeServerBusy)
		return
	}
	network.ResponseSuccess(c, "修改昵称成功")
}

func GetUserListHandler(c *gin.Context) {
	logic.GetUserList(c)
}

func GetAllUser(c *gin.Context) {
	logic.GetAllUser(c)
}

func DeleteUserHandler(c *gin.Context) {
	logic.DeleteUser(c)
}
